<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-11-27
 * Time: 9:20
 */

namespace app\common\model;


use think\Db;
use think\Model;

class Projectm extends Model
{
    /**
     * 证券列表
     */
    public static function projectList($map = [], $page)
    {
        return Db::name('project')
            ->where($map)
            ->field('*')
            ->order('p_id DESC')
            ->paginate(15,false, ['page' => $page]);

    }



    /**
     * 项目详情
     */
    public static function detail($pid)
    {
        return  Db::name('project')->where('p_id', $pid)->find();
    }

    /**
     * PC端添加证券
     */
    public static function pcAddProject($data)
    {
        $time = time();
        return Db::name('project')->insertGetId([
            'p_name'       => $data['p_name'],
            'p_photo'      => $data['photo'][0],
            'p_profit'     => $data['p_profit'],
            'p_num'        => $data['p_num'],
            'p_status'     => $data['p_status'],
            'p_create_time'=> $time,
            'p_update_time'=> $time
        ]);
    }
    /**
     *证券禁用启用
     */
    public static function editProject($id, $status)
    {
        return Db::name('project')->where('p_id', $id)->update(['p_status' => $status, 'p_update_time' => time()]);
    }

    /**
     * PC端  修改证券
     */
    public static function pcEditProject($data)
    {
        $time = time();
        return Db::name('project')->where('p_id', $data['p_id'])->update([
            'p_name'       => $data['p_name'],
            'p_photo'      => $data['p_photo'][0],
            'p_profit'     => $data['p_profit'],
            'p_num'        => $data['p_num'],
            'p_status'     => $data['p_status'],
            'p_update_time'=> $time
        ]);
    }

    /**
     * 所有证券  用户每天发布抢购数量的定时器
     */
    public static function getProjectUseTimer()
    {
        return Db::name('project')->field('p_id')->select();
    }
    /**
     * 修改 证券里面发布的抢购数量
     */
    public static function updateProjectNum($pid, $num)
    {
        return Db::name('project')->where('p_id', $pid)->update([
            'p_num' => $num,
            'p_update_time' => time()
        ]);
    }
    /**
     * ==============================================================================================================================
     */

    /**用户抢购证券
     * @param $pid  证券ID
     * @param $uid  用户ID
     */
    public static function userBuyProject($pid, $uid)
    {
        $money = Db::name('user')->where('u_id', $uid)->value('u_dollar_balance');
        if (100 > floatval($money)) {
                return -1;
        }
        $project = Db::name('project')->where('p_id', $pid)->field('p_name, p_num')->find();
        if (intval($project['p_num']) < 1) {
            return -2;
        }
        //查询该用户之前有没有买过该证券
        $usid = Db::name('user_securities')->where('us_uid', $uid)->where('us_pid', $pid)->value('us_id');
        if ($usid) {//购买过
            Db::startTrans();
            try {
                Db::name('project')->where('p_id',$pid)->setDec('p_num', 1);
                Db::name('user_securities')->where('us_id', $usid)->setInc('us_securities_money', 100);
                Db::name('user_securities')->where('us_id', $usid)->update([
                   'us_update_time'   => time()
                ]);
                Db::name('user')->where('u_id', $uid)->setDec('u_dollar_balance', 100);
                Db::commit();
                return $project['p_name'];
            } catch(\Exception $e){
                Db::rollback();
                return false;
            }
        } else {//第一次抢购
            Db::startTrans();
            try {
                Db::name('project')->where('p_id',$pid)->setDec('p_num', 1);
                Db::name('user_securities')->insertGetId([
                    'us_uid'                    => $uid,
                    'us_pid'                    => $pid,
                    'us_securities_money'       => 100,
                    'us_yesterday_earnings'     => 0,
                    'us_create_time'            => time(),
                    'us_update_time'            => time(),
                ]);
                Db::name('user')->where('u_id', $uid)->setDec('u_dollar_balance', 100);
                Db::commit();
                return $project['p_name'];
            } catch(\Exception $e){
                Db::rollback();
                return false;
            }
        }
    }
    /**
     * 入金
     */
    public static function userEnterEntry($data)
    {
        $money = Db::name('user')->where('u_id', $data['u_id'])->value('u_dollar_balance');
        if (floatval($data['money']) > floatval($money)) {
            return -1;
        }
        $usId = Db::name('user_securities')->where('us_uid', $data['u_id'])->where('us_pid', $data['p_id'])->value('us_id');
        $time = time();
        if ($usId) {
            Db::startTrans();
            try {
                Db::name('user_securities')->where('us_id', $usId)->setInc('us_securities_money', $data['money']);
                Db::name('user')->where('u_id', $data['u_id'])->setDec('u_dollar_balance', $data['money']);
                Db::name('user')->where('u_id', $data['u_id'])->update(['u_update_time' => $time]);
                Db::commit();
                return true;
            } catch(\Exception $e){
                Db::rollback();
                return false;
            }
        } else {
            Db::startTrans();
            try {
                Db::name('user_securities')->insertGetId([
                    'us_uid'                    => $data['u_id'],
                    'us_pid'                    => $data['p_id'],
                    'us_securities_money'       => $data['money'],
                    'us_create_time'            => $time,
                    'us_update_time'            => $time,
                ]);
                Db::name('user')->where('u_id', $data['u_id'])->setDec('u_dollar_balance', $data['money']);
                Db::name('user')->where('u_id', $data['u_id'])->update(['u_update_time' => $time]);
                Db::commit();
                return true;
            } catch(\Exception $e){
                Db::rollback();
                return false;
            }
        }
    }

}